package aaaBrowser;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.swt.browser.ProgressEvent;
import org.eclipse.swt.browser.ProgressListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import aaaBrowser.model.PushModel;
import aaaBrowser.model.RegModel;

import com.attilax.Closure;
import com.attilax.core;
import com.attilax.io.filex;
import com.attilax.io.pathx;

public class LoginAndPub extends Thread
{
    
    public LoginAndPub()
    {
        this.capt = new CaptX();
    }
    
    public static void main(String[] args)
    {
        new LoginAndPub().start();
    }
    
    final Display display = new Display();
    
    atiBrowserExample atiBrowserExampleinstance;
    
    public void start(PushModel push)
    {
        
        Shell shell = new Shell(display);
        shell.setLayout(new FillLayout());
        shell.setText("Browser example");
        // shell.setVisible(false);
        // shell.setSize(1, 1);
        atiBrowserExampleinstance = new atiBrowserExample(shell);
        
        shell.open(); // shell.setVisible(false);
        
        // core.execMeth_Ays(new Runnable() {
        //
        // @Override
        // public void run() {
        // atiBrowserExampleinstance.addProgressListener(pathx
        // .classPath(LoginAndPub.class) + "\\login.js");
        // atiBrowserExampleinstance
        // .gotoUrl("http://demo.destoon.com/v5.0/member/login.php?a0");
        //
        // }
        // }, "--");
        
         //login();
      //  final PushModel push=new PushModel();
       // push.setCiacturl("http://dt5gbk.weijicn.cn/member/my.php?mid=5&action=add");
        pub(push);
        // atiBrowserExampleinstance.ini();
        
        // atiBrowserExampleinstance.addProgressListener(pathx
        // .classPath(LoginAndPub.class) + "\\login.js");
        
        while (!shell.isDisposed())
        {
            if (!display.readAndDispatch())
                display.sleep();
        }
        // icon.dispose();
        atiBrowserExampleinstance.dispose();
        display.dispose();
    }
    
    private void login(final String url)
    {
        core.execMeth_Ays(new Runnable()
        {
            
            @Override
            public void run()
            {
                System.out.println("=-ajsy");
                display.getDefault().asyncExec(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        
                        // atiBrowserExampleinstance
                        // .gotoUrl("http://www.baidu.com");
                        atiBrowserExampleinstance.gotoUrl("http://atidestoon.com/member/login.php");
                        atiBrowserExampleinstance.addProgressListener(pathx.classPath(LoginAndPub.class) + "\\login.js",
                            new Closure()
                            {
                                
                                @Override
                                public Object execute(Object arg0)
                                    throws Exception
                                {
                                    final PushModel push=new PushModel();
                                   push.setCiacturl("http://dt5gbk.weijicn.cn/member/my.php?mid=5&action=add");
                                   pub(push);
                                    return null;
                                }
                                
                            });
                        
                        atiBrowserExampleinstance.regJsCallbackMeth("getCaptch", new Closure()
                        {
                            
                            @Override
                            public Object execute(Object arg0)
                                throws Exception
                            {
                                setCaptch();
                                return null;
                            }
                        });
                        
                        atiBrowserExampleinstance.gotoUrl();
                        // display.getDefault().asyncExec(new Runnable(){
                        //
                        // @Override
                        // public void run() {
                        // atiBrowserExampleinstance
                        // .gotoUrl();
                        //
                        // }});
                        
                        // atiBrowserExampleinstance.waitFinish(display);
                        
                    }
                });
                
            }
        },
            "");
    }
    
    CaptX capt;
    
    public void setCaptch()
    {
        
        execMeth_AysGui(new Runnable()
        {
            
            @Override
            public void run()
            {
                String captchCode = capt.getTxt();
                
                String js = filex.read(pathx.classPath(Reg.class) + "\\set_captch.js");
                js = js.replaceAll("@capt", captchCode);
                // "1234";
                System.out.println("exe js:" + js);
                atiBrowserExampleinstance.browser.execute(js);
                
            }
        }, "setcapt");
        
    }
    
    private void pub(final PushModel push)
    {
        core.execMeth_Ays(new Runnable()
        {
            
            @Override
            public void run()
            {
                System.out.println(" start pub");
                
                try
                {
                    Thread.sleep(3000);
                }
                catch (InterruptedException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                display.getDefault().asyncExec(new Runnable()
                {
                    
                    @Override
                    public void run()
                    {
                        String url1 = push.getCiacturl();
                        atiBrowserExampleinstance.gotoUrl(url1);
                        atiBrowserExampleinstance.addProgressListener(pathx.classPath(LoginAndPub.class) + "\\pub.js",
                            new ClosureEmpty()
                        );
                     
                        atiBrowserExampleinstance.regJsCallbackMeth("getCaptch", new Closure()
                        {
                            
                            @Override
                            public Object execute(Object arg0)
                                throws Exception
                            {
                                setInfo(push);
                               // setCaptch();
                                return null;
                            }
                        });
                       
                        atiBrowserExampleinstance.gotoUrl();
                        System.out.println(" start pub finish");
                    }
                });
            }
        },
            "--");
        
    }
    
    public void setInfo( PushModel push)
    {
        String js = filex.read(pathx.classPath(LoginAndPub.class) + "\\pub.js");
        js = js.replace("@title", push.getTitle());
        js = js.replace("@catid", push.getCatid());
        js = js.replace("@thumb", push.getThumb());
        js = js.replaceAll("@content", push.getContent());
        atiBrowserExampleinstance.browser.execute(js);
        
    }
    
    private void execMeth_AysGui(final Runnable runnable, String string)
    {
        core.execMeth_Ays(new Runnable()
        {
            
            @SuppressWarnings("static-access")
            @Override
            public void run()
            {
                System.out.println("=-ajsy");
                // Display default1 = display.getDefault();
                // runnable.run();
                Display.getDefault().asyncExec(runnable);
                // display.asyncExec(runnable);
            }
        }, string);
        
    }
    
}
